<?php 
session_start();
include '../connection/connection.php';
function getlang(){
		$lang = $_SESSION['lang'];
		if (empty($lang) || $lang == "EN") {
			$value = "../value/value_en.php";
		} else if($lang == "VN") {
			$value = "../value/value_vn.php";
		}
		else if($lang == "KR"){
			$value = "../value/value_kr.php";
		}
		return $value;
	}
	
	$value = getlang();
	include($value);
	$lang = $_SESSION['lang'];
	if($lang=='')
		$lang="EN";
		
if(isset($_REQUEST['action'])){
	if($_REQUEST['action']=="addLicense"){
		$email=$_REQUEST['email'];
		$license=$_REQUEST['license'];
		mysql_query("INSERT INTO license (email,id_type_license,status) VALUES ('$email', '$license', '1')");
	}
	if($_REQUEST['action']=="delLicense"){
		$id_license=$_REQUEST['id_license'];
		mysql_query("DELETE FROM license WHERE (id_license='$id_license')");
	}
	if($_REQUEST['action']=="editLicense"){
		$id_license=$_REQUEST['id_license'];
		$date_stop2=$_REQUEST['date_stop'];
		$date_stop1=$_REQUEST['date_stop1'];
		
		if ( strtotime($date_stop2) > strtotime($date_stop1) ) {
			mysql_query("UPDATE license SET date_stop='$date_stop2' WHERE (id_license='$id_license')");
		}else{
		//echo "ko update duoc";
		//echo $date_stop1;
		//echo $date_stop2;
		}
	}
}


?>
    		<?php 
				$query=mysql_query("SELECT e.email,t.time,e.status,e.date_start,e.date_stop,e.id_license 
									FROM (SELECT u.email,u.role,l.id_type_license,l.status,l.date_start,l.date_stop,l.id_license
												FROM user_info u 
															LEFT JOIN license l ON u.email=l.email) e 
												LEFT JOIN type_license t ON e.id_type_license=t.id_type_license
									WHERE e.role=0;");
				 $list_license=array();
				 $email_temp="";
				 while ($row=mysql_fetch_array($query)){
				 	$list_license[$row['email']][]=array($row['time'],$row['status'],$row['date_start'],$row['date_stop'],$row['id_license']);
				 }
				 mysql_close($connection);
			?>
	       <table width="800" cellpadding="0" cellspacing="0" class="mtable">
	    	<thead>
	        	<tr>
	                <td align="center"><?=$address_email?></td>
	                <td align="center"><?=$type_license?></td>
	                <td align="center"><?=$namestatus?></td>
	                <td align="center"><?=$date_start?></td>
	                <td align="center"><?=$date_stop?></td>
	                <td align="center"></td>
	                <td align="center"></td>
	            </tr>
	        </thead>
	        <?php 
	        foreach ($list_license as $email => $license){
	        	
	        	for($i=0;$i<count($license);$i++){	
	        	?>
	        	<?php
	        			if($license[$i][0]==0){
	        				$time=$free;
	        			}else{
	        				$time=$license[$i][0]." ".$month;
	        			}
	        			
	        			if($license[$i][1]==1){
	        				$color='white';
	        				$status=$status1;
	        			}
	        			if($license[$i][1]==""){
	        				$color='white';
	        				$status="";
	        				$time="";
	        			}
	        			if($license[$i][1]==2){
	        				$color='#B2DAF7';
	        				$status=$status2;
	        			}
	        			if($license[$i][1]==3){
	        				$color='#F1DFDC';
	        				$status=$status3;
	        			}
	        	if($i==0){
	        			
	        		
	        		?>
		        <tr>
		        		<td rowspan="<?=count($license)?>"><?=$email?></td>
		        	
		            		<td  style="background-color: <?=$color?>;" align="center"><?=$time?></td>
			        		<td  style="background-color: <?=$color?>;" align="center"><?=$status?></td>
		            		<td  style="background-color: <?=$color?>;" align="center"><?=$license[$i][2]?></td>
		            		<td  style="background-color: <?=$color?>;" align="center"><?=$license[$i][3]?></td>
		            		<td style="background-color: <?=$color?>;" align="center">
		            			<?php if($license[$i][1]==2 && $license[$i][0]!=0){?><a href="#?w=260" onclick="calleditLicense('<?=$license[$i][4]?>','<?=$license[$i][3]?>',this)" rel="popup_name1" class="poplight"><img src="images/edit.png" title="<?=$edit?>" /></a><?php }?>
		            			<?php if($license[$i][1]==3){?><a onclick="delLicense('<?=$license[$i][4]?>')"><img src="images/xoa.png" title="<?=$del2?>" /></a><?php }?>
		            		</td>
			            <td rowspan="<?=count($license)?>">
			            	<a href="#?w=300" onclick="calladdLicense('<?=$email?>',this)" rel="popup_name" class="poplight" style="padding-left:10px"><img src="images/them.png" title="<?=$them?>" /></a>
			         	</td>
		        </tr>
		        <?php }else{?>
	        	<tr>
		        		<td  style="background-color: <?=$color?>;" align="center"><?=$time?></td>
		        		<td  style="background-color: <?=$color?>;" align="center"><?=$status?></td>
	            		<td  style="background-color: <?=$color?>;" align="center"><?=$license[$i][2]?></td>
	            		<td  style="background-color: <?=$color?>;" align="center"><?=$license[$i][3]?></td>
	            		<td style="background-color: <?=$color?>;" align="center" >
		            		<?php  if($license[$i][1]==2 && $license[$i][0]!=0){?><a href="#?w=260" onclick="calleditLicense('<?=$license[$i][4]?>','<?=$license[$i][3]?>',this)" rel="popup_name1" class="poplight"><img src="images/edit.png" title="<?=$edit?>" /></a><?php }?>
		            		<?php  if($license[$i][1]==3){?><a onclick="delLicense('<?=$license[$i][4]?>')"><img src="images/xoa.png" title="<?=$del2?>" /></a><?php }?>
	            		</td>
	        	</tr>
	      	<?php } }
	        	}?>
	    </table>